package com.aiyige.page.my.favorite.adapter;

import android.os.Build;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.Subject;
import com.aiyige.model.moment.backup.CreatorBackup;
import com.aiyige.model.moment.backup.MediaSummary;
import com.aiyige.model.moment.entity.MajorCourse;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.model.moment.entity.TrainingCourse;
import com.aiyige.page.my.favorite.SearchTypeFavoritePage;
import com.aiyige.page.my.favorite.model.SearchTypeMoment;
import com.aiyige.track.TrackEvent;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuyakun on 2017/10/17.
 */

public class SearchFavoriteAdapter extends BaseQuickAdapter<SearchTypeMoment, BaseViewHolder> {
    SearchValueCallback callback;
    TrackEvent trackEvent = new TrackEvent();

    public SearchFavoriteAdapter() {
        super(new LinkedList<SearchTypeMoment>());
    }


    @Override
    protected void convert(BaseViewHolder helper, SearchTypeMoment item) {

        if (item.isShowTitle()) {
            ((HasTitleHolder) helper).bindData(item);
        } else {
            ((MyHolder) helper).bindData(item);
        }
    }

    @Override
    protected int getDefItemViewType(int position) {
        return mData.get(position).isShowTitle() ? 0 : 1;
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder = viewType == 0 ? new HasTitleHolder(getItemView(R.layout.item_has_title_favorite, parent)) :
                new MyHolder(getItemView(R.layout.item_favorite, parent));
        return baseViewHolder;
    }

    @Override
    public void setNewData(@Nullable List<SearchTypeMoment> data) {
        super.setNewData(data);
    }

    public class MyHolder extends BaseViewHolder {

        @BindView(R.id.iv_item_selection)
        ImageView ivItemSelection;
        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.tv_content_count)
        TextView tvContentCount;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.iv_head)
        ImageView ivHead;
        @BindView(R.id.tv_username)
        TextView tvUsername;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.ll_containerLayout)
        LinearLayout llContainerLayout;


//        @BindView(R.id.tv_tag)
//        TextView tvTag;
        @BindView(R.id.ll_photos)
        LinearLayout llPhotoLayout;
        @BindView(R.id.iv_image_photo1)
        ImageView ivPhoto1;
        @BindView(R.id.iv_image_photo2)
        ImageView ivPhoto2;
        @BindView(R.id.iv_image_photo3)
        ImageView ivPhoto3;

        @BindView(R.id.fl_video_count_time)
        FrameLayout flVideoCountTime;
        @BindView(R.id.ll_class_valid_info)
        LinearLayout llclassValidInfo;

        @BindView(R.id.iv_class_valid_info_icon)
        ImageView tvClassValidInfoImage;
        @BindView(R.id.tv_class_valid_info)
        TextView tvClassValidInfo;
        @BindView(R.id.tv_subject_tag)
        TextView tvSubjectTag;


        public MyHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }


        public void bindData(SearchTypeMoment data) {
            final Moment moment = data.getMoment();
            if (moment != null) {
                Spanned result;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    result = Html.fromHtml(moment.getTitle(), Html.FROM_HTML_MODE_LEGACY);
                } else {
                    result = Html.fromHtml(moment.getTitle());
                }
                tvTitle.setText(result);
                tvTitle.setText(data.isSearch()?result:moment.getTitle());
//                if (!TextUtils.isEmpty(moment.getSubject())) {
//                    tvTag.setVisibility(View.VISIBLE);
//                    tvTag.setText(Subject.getSubjectName(moment.getSubject()));
//                }
                if (moment.getCreatorBackup() != null) {
                    CreatorBackup creatorBackup = moment.getCreatorBackup();
                    if (creatorBackup.getAvatar() != null) {
                        GlideUtil.with(mContext).small().loadAvatar(creatorBackup.getAvatar()).into(ivHead);
                        tvUsername.setText(creatorBackup.getName());
                    }
                }
                if (!ListUtil.isEmpty(moment.getCover())) {
                    GlideUtil.with(mContext).big().loadCover(moment.getCover().get(0)).into(ivCover);
                }
                MediaSummary mediaSummary = moment.getMediaSummary();
                if (mediaSummary != null) {
                    tvContentCount.setVisibility(mediaSummary.getCount() > 0 ? View.VISIBLE : View.INVISIBLE);
                    if (data.getSubject().equals(Subject.video_course) || (data.getSubject().equals(Subject.video))) {
                        tvContentCount.setText(mContext.getString(R.string.content_count_value, mediaSummary.getCount() + ""));
                        tvTime.setVisibility(View.VISIBLE);
                        flVideoCountTime.setVisibility(View.VISIBLE);
                        tvTime.setText(TimeUtils.getDurationTimeForFavorite(mediaSummary.getDuration()));
                        flVideoCountTime.setVisibility(View.VISIBLE);
                    } else if (data.getSubject().equals(Subject.photo)) {
                        tvContentCount.setText(mContext.getString(R.string.photo_count_value, mediaSummary.getCount() + "") + "");
                    } else {
                        flVideoCountTime.setVisibility(View.INVISIBLE);
                    }

                    if (data != null && data.getSubject().equals(Subject.photo)) {
                        tvContentCount.setVisibility(View.INVISIBLE);
                    }

                }

                // 专业课程
                if (data.getSubject().equals(Subject.major_course)) {
                    MajorCourse majorCourse = JsonUtil.toObject(moment.getMoreBackup(), MajorCourse.class);
                    if (majorCourse != null) {
                        tvClassValidInfoImage.setVisibility(View.VISIBLE);
                        llclassValidInfo.setVisibility(View.VISIBLE);
                        tvSubjectTag.setText(majorCourse.getClassCount() + mContext.getString(R.string.class_count));
                        tvClassValidInfo.setText(mContext.getString(R.string.class_valid_info_value, majorCourse.getCityName(moment.getCityId()), majorCourse.getStartClassTime()));
                    }
                }
                // 培训班
                if (data.getSubject().equals(Subject.training_course)) {
                    TrainingCourse trainingCourse = JsonUtil.toObject(moment.getMoreBackup(), TrainingCourse.class);
                    if (trainingCourse != null) {
                        tvClassValidInfoImage.setVisibility(View.VISIBLE);
                        llclassValidInfo.setVisibility(View.VISIBLE);
                        tvSubjectTag.setText(trainingCourse.getClassCount() + mContext.getString(R.string.class_count));
                        tvClassValidInfo.setText(mContext.getString(R.string.class_valid_info_value, trainingCourse.getCityName(), trainingCourse.getStartClassTime()));
                    }
                }
                llContainerLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        String howLong = TrackUtil.getHowLong(moment.getOnsellTime());
//                        if (!TextUtils.isEmpty(howLong)) {
//                            HashMap<String, String> hashMap = new HashMap<>();
//                            hashMap.put("time_distance", howLong);
//                            trackEvent.sendEvent(MyApp.getAppContext(), MY_FAVORITE_CONTENT_VIEW_DATE,
//                                    MY_FAVORITE_CONTENT_VIEW_DATE_LABEL, hashMap);
//                        }
                        Router.start(moment.getRouter());
                    }
                });
                ivItemSelection.setSelected(data.isSelected());
            }
        }
    }

    private void showDialog() {
        ((BaseActivity) mContext).showLoading();
    }

    private void dismissDialog() {
        ((BaseActivity) mContext).hideLoading();
    }


    public class HasTitleHolder extends BaseViewHolder {

//        @BindView(R.id.iv_item_selection)
//        ImageView ivItemSelection;
        @BindView(R.id.tv_type_name)
        TextView tvTypeName;
        @BindView(R.id.tv_more)
        TextView tvMore;
        @BindView(R.id.iv_right)
        ImageView ivRight;
        @BindView(R.id.ll_more)
        LinearLayout llMore;
        @BindView(R.id.rl_type)
        RelativeLayout rlType;
        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.tv_content_count)
        TextView tvContentCount;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.iv_head)
        ImageView ivHead;
        @BindView(R.id.tv_username)
        TextView tvUsername;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.ll_containerLayout)
        LinearLayout llContainerLayout;

//        @BindView(R.id.tv_tag)
//        TextView tvTag;
        @BindView(R.id.ll_photos)
        LinearLayout llPhotoLayout;
        @BindView(R.id.iv_image_photo1)
        ImageView ivPhoto1;
        @BindView(R.id.iv_image_photo2)
        ImageView ivPhoto2;
        @BindView(R.id.iv_image_photo3)
        ImageView ivPhoto3;

        @BindView(R.id.fl_video_count_time)
        FrameLayout flVideoCountTime;
        @BindView(R.id.ll_class_valid_info)
        LinearLayout llclassValidInfo;
        @BindView(R.id.iv_class_valid_info_icon)
        ImageView tvClassValidInfoImage;
        @BindView(R.id.tv_class_valid_info)
        TextView tvClassValidInfo;
        @BindView(R.id.tv_subject_tag)
        TextView tvSubjectTag;

        public HasTitleHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }


        public void bindData(final SearchTypeMoment data) {

            final Moment moment = data.getMoment();
            if (!TextUtils.isEmpty(data.getSubject())) {
                tvTypeName.setText(Subject.getSubjectName(data.getSubject()));
            }
            // type标题只显示一次
            rlType.setVisibility(data.isShowTitle() ? View.VISIBLE : View.GONE);
            if (moment == null || moment.getId() == null) {
                llContainerLayout.setVisibility(View.GONE);
                tvMore.setText(mContext.getString(R.string.no_search_result));
                ivRight.setVisibility(View.GONE);
            } else {
                if (data.isHasMore()) {
                    tvMore.setText(mContext.getString(R.string.more));
                    ivRight.setVisibility(View.VISIBLE);
                    llMore.setVisibility(View.VISIBLE);
                    llMore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            SearchTypeFavoritePage.start(data.getSubject(), callback.getSearchValue());
                        }
                    });
                } else {
                    llMore.setVisibility(View.INVISIBLE);
                    ivRight.setVisibility(View.INVISIBLE);
                }
                llContainerLayout.setVisibility(View.VISIBLE);
                if (moment != null) {
                    Spanned result;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        result = Html.fromHtml(moment.getTitle(), Html.FROM_HTML_MODE_LEGACY);
                    } else {
                        result = Html.fromHtml(moment.getTitle());
                    }
                    tvTitle.setText(data.isSearch()?result:data.getMoment().getTitle());
//                    if (!TextUtils.isEmpty(moment.getSubject())) {
//                        tvTag.setVisibility(View.VISIBLE);
//                        tvTag.setText(Subject.getSubjectName(moment.getSubject()));
//                    }
                    if (moment.getCreatorBackup() != null) {
                        CreatorBackup creatorBackup = moment.getCreatorBackup();
                        if (creatorBackup.getAvatar() != null) {
                            GlideUtil.with(mContext).small().loadAvatar(creatorBackup.getAvatar()).into(ivHead);
                            tvUsername.setText(creatorBackup.getName());
                        }
                    }
                    if (!ListUtil.isEmpty(moment.getCover())) {
                        GlideUtil.with(mContext).big().loadCover(moment.getCover().get(0)).into(ivCover);
                    }
                    MediaSummary mediaSummary = moment.getMediaSummary();
                    if (mediaSummary != null) {
                        tvContentCount.setVisibility(mediaSummary.getCount() > 0 ? View.VISIBLE : View.INVISIBLE);
                        if (data.getSubject().equals(Subject.video_course) || (moment.getSubject().equals(Subject.video))) {
                            tvContentCount.setText(mContext.getString(R.string.content_count_value, mediaSummary.getCount() + ""));
                            // 收藏“视频”视频封面下面多显示了时长，应只显示右下角“共n集”，不显示时长，半透明背景只在右下角
//                            tvTime.setVisibility(View.VISIBLE);
//                            tvTime.setText(TimeUtils.getDurationTimeForFavorite(mediaSummary.getDuration()));
                            flVideoCountTime.setVisibility(View.VISIBLE);
                            tvSubjectTag.setVisibility(View.INVISIBLE);
                        } else if (data.getSubject().equals(Subject.photo)) {
                            tvContentCount.setText(mContext.getString(R.string.photo_count_value, mediaSummary.getCount() + "") + "");
                        } else {
//                            tvTime.setVisibility(View.INVISIBLE);
                            tvSubjectTag.setVisibility(View.VISIBLE);
                            flVideoCountTime.setVisibility(View.INVISIBLE);
                        }
                        if (data != null && data.getSubject().equals(Subject.photo)) {
                            tvContentCount.setVisibility(View.INVISIBLE);
                        }
                    }

                    // 专业课程
                    if (data.getSubject().equals(Subject.major_course)) {
                        MajorCourse majorCourse = JsonUtil.toObject(moment.getMoreBackup(), MajorCourse.class);
                        if (majorCourse != null) {
                            tvClassValidInfoImage.setVisibility(View.VISIBLE);
                            llclassValidInfo.setVisibility(View.VISIBLE);
                            tvSubjectTag.setText(majorCourse.getClassCount() + mContext.getString(R.string.class_count));
                            tvClassValidInfo.setText(mContext.getString(R.string.class_valid_info_value, majorCourse.getCityName(moment.getCityId()), majorCourse.getStartClassTime()));
                        }
                    }
                    // 培训班
                    if (data.getSubject().equals(Subject.training_course)) {
                        TrainingCourse trainingCourse = JsonUtil.toObject(moment.getMoreBackup(), TrainingCourse.class);
                        if (trainingCourse != null) {
                            tvClassValidInfoImage.setVisibility(View.VISIBLE);
                            llclassValidInfo.setVisibility(View.VISIBLE);
                            tvSubjectTag.setText(trainingCourse.getClassCount() + mContext.getString(R.string.class_count));
                            tvClassValidInfo.setText(mContext.getString(R.string.class_valid_info_value, trainingCourse.getCityName(), trainingCourse.getStartClassTime()));

                        }
                    }
//                    ivItemSelection.setSelected(data.isSelected());
                    llContainerLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                            String howLong = TrackUtil.getHowLong(moment.getOnsellTime());
//                            if (!TextUtils.isEmpty(howLong)) {
//                                HashMap<String, String> hashMap = new HashMap<>();
//                                hashMap.put("time_distance", howLong);
//                                trackEvent.sendEvent(MyApp.getAppContext(), MY_FAVORITE_CONTENT_VIEW_DATE,
//                                        MY_FAVORITE_CONTENT_VIEW_DATE_LABEL, hashMap);
//                            }
                            Router.start(moment.getRouter());
                        }
                    });
                }
            }
        }
    }


    public void setSearchValueCallback(SearchValueCallback callback) {
        this.callback = callback;
    }

    public interface SearchValueCallback {
        String getSearchValue();
    }


}
